package net.jjjshop.common.service.lbt.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.jjjshop.common.constants.ConfConstants;
import net.jjjshop.common.entity.lbt.*;
import net.jjjshop.common.entity.supplier.SupplierUser;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.mapper.lbt.LbTransferRecordMapper;
import net.jjjshop.common.service.lbt.LbSysConfigService;
import net.jjjshop.common.service.lbt.LbTransferRecordService;
import net.jjjshop.common.service.lbt.LbUserWalletService;
import net.jjjshop.common.service.lbt.UserBillService;
import net.jjjshop.common.service.supplier.SupplierUserService;
import net.jjjshop.common.service.user.UserService;
import net.jjjshop.common.util.BigDecimalUtil;
import net.jjjshop.framework.common.exception.BusinessException;
import net.jjjshop.framework.util.LoginUtil;
import net.jjjshop.framework.util.PasswordUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 用户转账记录表 接口实现类
 */

@Service("lbTransferRecordService")
public class LbTransferRecordServiceImpl extends ServiceImpl<LbTransferRecordMapper, LbTransferRecordEntity> implements LbTransferRecordService {

    @Autowired
    private LbTransferRecordMapper dao;
    @Autowired
    private UserService userService;

    @Autowired
    private LbUserWalletService walletService;
    @Autowired
    private UserBillService userBillService;

//    @Autowired
//    private LbUnionStoreService lbUnionStoreService;
    @Autowired
    private LbSysConfigService sysConfigService;
    @Autowired
    private SupplierUserService supplierUserService;

    @Override
    public IPage<LbTransferRecordEntity> getList(Integer type, Integer page, Integer limit) {
        Page<JybTradeOrderEntity> pages = new Page<>(page,limit);
        if (null == type){
            return dao.queryList(pages,null,null);
        }
        if (type == 1){
            //查询自己转账记录
            return dao.queryList(pages,userService.getUserId(),null);
        }
        if (type == 2){
            //查询自己收款记录
            User user = userService.getById(LoginUtil.getUserId());
            return dao.queryList(pages,null,user.getMobile());
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addRecord(LbTransferRecordEntity bean) {
        bean.setCreateTime(new Date());
        bean.setUid(userService.getUserId());
        User byPhone = userService.getByAccount(bean.getReceiveAccount());
        if (null == byPhone){
            throw new RuntimeException("用户不存在");
        }

        //处理转出人账户
        LbUserWalletEntity dWallet = walletService.getUserWalletInfo(bean.getUid());
        if (null == dWallet || null == dWallet.getId()) {
            throw new RuntimeException("您当前没有钱包");
        }
//        BigDecimal total = BigDecimalUtil.mul(bean.getNum(), new BigDecimal(ConfConstants.INTEGRAL_TRANSFER));
        //扣除手续费百分之比
//        BigDecimal allPrice = BigDecimalUtil.addDouble(bean.getNum(),total );

        if (null == dWallet.getScore() || dWallet.getScore().doubleValue() < bean.getNum().doubleValue()) {
            throw new RuntimeException("您的余额不足转出");
        }
       if ( !userBillService.addBillRecord("权益卡转出", ConfConstants.INTEGRAL_TYPE,0,bean.getNum(),userService.getUserId(),null)){
           throw new RuntimeException("转出异常");
       }
        if ( !userBillService.addBillRecord("权益卡转入",ConfConstants.INTEGRAL_TYPE,1,bean.getNum(),byPhone.getUserId(),null)){
            throw new RuntimeException("转出异常");
        }
        dWallet.setScore(BigDecimalUtil.subDouble(dWallet.getScore(), bean.getNum()));
        if (!walletService.updateById(dWallet)) {
            throw new RuntimeException("转出异常");
        }
        LbUserWalletEntity walletInfo = walletService.getUserWalletInfo(byPhone.getUserId());
        if (null == walletInfo || null == walletInfo.getId()) {
            throw new RuntimeException("对方接收失败");
        }
        walletInfo.setScore(BigDecimalUtil.addDouble(walletInfo.getScore(), bean.getNum()));
        if (!walletService.updateById(walletInfo)) {
            throw new RuntimeException("对方接收异常");
        }
        if (!save(bean)){
            throw new RuntimeException("转出异常");
        }
        bean.setUid(byPhone.getUserId());
        bean.setPm(1);
        if (!save(bean)){
            throw new RuntimeException("转出异常");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addPayment(CodePaymentRequest payBean) {
        User user = userService.getById(LoginUtil.getUserId());
        if (StringUtils.isEmpty(user.getPayPass())){
            throw new RuntimeException("请先设置交易密码");
        }
        String oldStr = DigestUtils.md5Hex(payBean.getPayPass());
        if (!user.getPayPass().equals(oldStr)){
            throw new RuntimeException("交易密码不对");
        }
        LbUserWalletEntity dUser = walletService.getUserWalletInfo(userService.getUserId());
        if (null == dUser || null == dUser.getId()) {
            throw new RuntimeException("您当前没有钱包");
        }
        //查询收款人账户
        LbUserWalletEntity dWallet = walletService.getUserWalletInfo(payBean.getUid());
        if (payBean.getType().intValue() == 1){
            if (null == dUser.getMoney() || dUser.getMoney().doubleValue() <payBean.getMoney().doubleValue()) {
                throw new RuntimeException("您的余额不足");
            }

            if (null == dWallet || null == dWallet.getId()) {
                throw new RuntimeException("收款人账户不存在");
            }
            User bs = userService.getById(payBean.getUid());
//            SupplierUser fkr = supplierUserService.getOne(new LambdaQueryWrapper<SupplierUser>()
//                    .eq(SupplierUser::getUserName, user.getMobile()));
            if (!user.getMobile().equals("19999999999")&& !user.getMobile().equals("13116493346")) {



//                SupplierUser supplierUser = supplierUserService.getOne(new LambdaQueryWrapper<SupplierUser>()
//                        .eq(SupplierUser::getUserName, bs.getMobile()));
                if (StringUtils.isEmpty(user.getRegionAgent())  && StringUtils.isEmpty(bs.getRegionAgent())) {
                    throw new RuntimeException("会员不能互转");
                }
            }
//            //扣权益卡
//            BigDecimal total = BigDecimalUtil.mul(payBean.getMoney(), new BigDecimal(ConfConstants.INTEGRAL_TRANSFER));
//
//            if (null == dUser.getScore() || dUser.getScore().doubleValue() <total.doubleValue()) {
//                throw new RuntimeException("您的权益卡不足");
//            }
            //计算账户额度
            BigDecimal allPrice = BigDecimalUtil.addDouble(payBean.getMoney(),dWallet.getMoney() );

            dWallet.setMoney(allPrice);

            LbSysConfigEntity configEntity = sysConfigService.selectSysConfig();
            if (configEntity == null){
                throw new RuntimeException("转账配置异常");
            }
            BigDecimal money = payBean.getMoney();
            BigDecimal rateConf = configEntity.getTransferRate();
            BigDecimal userMoney  = dUser.getMoney();
            BigDecimal moneyRate  = BigDecimalUtil.mul(money, rateConf);
            BigDecimal userMoneySubRate  = BigDecimalUtil.subDouble(userMoney,moneyRate);

            if (money.compareTo(userMoney) > 0 ){
                throw new RuntimeException("转出数量不能大于账户余额");
            }
//
            if (money.doubleValue() > userMoney.doubleValue()){
                throw new RuntimeException("转出数量不能大于账户余额");
            }
            if (null == dUser.getMoney() || userMoneySubRate.doubleValue() < payBean.getMoney().doubleValue()) {
                throw new RuntimeException("您的余额不足,最多可转:"+userMoneySubRate);
            }
            if (null == dWallet || null == dWallet.getId()) {
                throw new RuntimeException("收款人账户不存在");
            }
//
//
//            //计算账户额度
//            BigDecimal allPrice = BigDecimalUtil.addDouble(money,dWallet.getMoney());
//
//            dWallet.setMoney(allPrice);
            //计算账户额度
//            BigDecimal payPrice = BigDecimalUtil.addDouble(payBean.getMoney(),total );

            if ( !userBillService.addBillRecord("来自"+user.getNickname()+"转账",ConfConstants.COMMISSION_TYPE,1,payBean.getMoney(),payBean.getUid(),null)){
                throw new RuntimeException("转账异常");
            }


//            if ( !userBillService.addBillRecord("给"+bs.getNickname()+"转账权益卡抵扣",ConfConstants.INTEGRAL_TYPE,0,total,userService.getUserId(),null)){
//                throw new RuntimeException("转账异常");
//            }

            if (!userBillService.addBillRecord("给"+bs.getNickname()+"转账",ConfConstants.COMMISSION_TYPE,0,payBean.getMoney(),userService.getUserId(),null)){
                throw new RuntimeException("转账异常");
            }
//            if (moneyRate.doubleValue() > 0){
//                if (!userBillService.addBillRecord("给"+bs.getNickname()+"转账手续费",ConfConstants.COMMISSION_TYPE,0,moneyRate,userService.getUserId(),null)){
//                    throw new RuntimeException("转账手续费记录异常");
//                }
//            }
//            dUser.setScore((BigDecimalUtil.subDouble(dUser.getScore(),total)));
            dUser.setMoney(BigDecimalUtil.subDouble(dUser.getMoney(), payBean.getMoney()));
            if (!user.getMobile().equals("19999999999")&& !user.getMobile().equals("13116493346")){
//                SupplierUser supplier = supplierUserService.getOne(new LambdaQueryWrapper<SupplierUser>()
//                        .eq(SupplierUser::getUserName, user.getMobile()));
                if (StringUtils.isEmpty(user.getRegionAgent())){
                    dUser.setMoney(BigDecimalUtil.subDouble(dUser.getMoney(), moneyRate));
                    dWallet.setMoney(BigDecimalUtil.addDouble(dWallet.getMoney(), moneyRate));
                    if ( !userBillService.addBillRecord("来自"+user.getNickname()+"转账手续费",ConfConstants.COMMISSION_TYPE,1,moneyRate,payBean.getUid(),null)){
                        throw new RuntimeException("转账异常");
                    }
                }

            }


//            if (!user.getMobile().equals("19999999999") && !user.getMobile().equals("13116493346")) {
//                configEntity.setDividendPool(BigDecimalUtil.addDouble(moneyRate,configEntity.getDividendPool()));
//                sysConfigService.updateById(configEntity);
//                userBillService.addBillRecord("转账贡献金额", ConfConstants.DIVIDEND_POOL, 1, moneyRate, userService.getUserId(), 2);
//            }
            if (!walletService.updateById(dWallet)) {
                throw new RuntimeException("转账异常");
            }
            if (!walletService.updateById(dUser)) {
                throw new RuntimeException("转账异常");
            }


        }
        if (payBean.getType().intValue() == 2){
            //扣权益卡
            BigDecimal total = BigDecimalUtil.mul(payBean.getMoney(), new BigDecimal(0));
            //计算账户额度
            BigDecimal allPrice = BigDecimalUtil.addDouble(payBean.getMoney(),total);
            if (null == dUser.getMoney() || dUser.getScore().doubleValue() < allPrice.doubleValue()) {
                throw new RuntimeException("您的权益卡余额不足");
            }
            //权益卡付款
            //查询收款人账户
            if (null == dWallet || null == dWallet.getId()) {
                throw new RuntimeException("收款人账户不存在");
            }

            dWallet.setScore(BigDecimalUtil.addDouble(dWallet.getScore(),payBean.getMoney()));
//            //计算账户额度
//            BigDecimal payPrice = BigDecimalUtil.addDouble(payBean.getMoney(),total );

            if ( !userBillService.addBillRecord("来自"+user.getNickname()+"转账",ConfConstants.INTEGRAL_TYPE,1,payBean.getMoney(),payBean.getUid(),null)){
                throw new RuntimeException("转账异常");
            }
            User bs = userService.getById(payBean.getUid());
            if ( !userBillService.addBillRecord("给"+bs.getNickname()+"转账",ConfConstants.INTEGRAL_TYPE,0,allPrice,userService.getUserId(),null)){
                throw new RuntimeException("转账异常");
            }
            dUser.setScore(BigDecimalUtil.subDouble(dUser.getScore(), allPrice));
            if (!walletService.updateById(dWallet)) {
                throw new RuntimeException("转账异常");
            }
            if (!walletService.updateById(dUser)) {
                throw new RuntimeException("转账异常");
            }
        }

//        if (payBean.getType().intValue() == 3){
//            LbUserWalletEntity tgStore =null;
//            if (null == dUser.getMoney() || dUser.getEntityMoney().doubleValue() <payBean.getMoney().doubleValue()) {
//                throw new RuntimeException("您的消费金不足");
//            }
//            LbUnionStoreEntity lbUnionStoreEntity = lbUnionStoreService.selectUserStore(payBean.getUid());
//            if (null == lbUnionStoreEntity || null == lbUnionStoreEntity.getId()){
//                throw new RuntimeException("该商家不存在");
//            }
//            tgStore = walletService.getUserWalletInfo(userService.getInfoByUid(payBean.getUid()).getSpreadUid());
//
//            if (null == tgStore){
//                throw new RuntimeException("商家信息异常");
//            }
//
//            //消费金支付
//            //查询收款人账户
//            if (null == dWallet || null == dWallet.getId()) {
//                throw new RuntimeException("收款人账户不存在");
//            }
////            BigDecimal total = BigDecimalUtil.mul(payBean.getMoney(), new BigDecimal(ConfConstants.INTEGRAL_TRANSFER));
//
//            BigDecimal skMoney = BigDecimalUtil.mul(payBean.getMoney(), new BigDecimal("0.8"));
//            //计算账户额度
//            BigDecimal allPrice = BigDecimalUtil.addDouble(skMoney,dWallet.getEntityMoney() );
//
//            dWallet.setEntityMoney(allPrice);
////            //计算账户额度
////            BigDecimal payPrice = BigDecimalUtil.addDouble(payBean.getMoney(),total );
//
//            if ( !userBillService.addBillRecord("二维码收款",ConfConstants.STORE_TYPE,1,skMoney,payBean.getUid(),null)){
//                throw new RuntimeException("付款异常");
//            }
//            if ( !userBillService.addBillRecord("扫码付款",ConfConstants.STORE_TYPE,0,payBean.getMoney(),userService.getUserId(),null)){
//                throw new RuntimeException("付款异常");
//            }
//            dUser.setEntityMoney(BigDecimalUtil.subDouble(dUser.getEntityMoney(), payBean.getMoney()));
//            if (!walletService.updateById(dWallet)) {
//                throw new RuntimeException("付款异常");
//            }
//            if (!walletService.updateById(dUser)) {
//                throw new RuntimeException("付款异常");
//            }
//            BigDecimal ffz2 = BigDecimalUtil.addDouble(payBean.getMoney(),new BigDecimal("0.2") );
//            tgStore.setMoney(BigDecimalUtil.addDouble(tgStore.getMoney(),ffz2));
//            if (!walletService.updateById(tgStore)) {
//                throw new RuntimeException("付款异常");
//            }
//            if ( !userBillService.addBillRecord("联盟店消费收益",ConfConstants.COMMISSION_TYPE,1,ffz2,tgStore.getUid(),null)){
//                throw new RuntimeException("付款异常");
//            }
//        }


        LbTransferRecordEntity bean = new LbTransferRecordEntity();
        bean.setNum(payBean.getMoney());
        bean.setCreateTime(new Date());
        bean.setUid(userService.getUserId());
        bean.setType(1);
        bean.setPm(0);
        bean.setRemarks(payBean.getRemarks());
        if (!save(bean)){
            throw new RuntimeException("付款异常");
        }
        bean.setUid(payBean.getUid());
        bean.setPm(1);
        if (!save(bean)){
            throw new RuntimeException("付款异常");
        }
        return true;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean dedicate(BigDecimal money, String payPass) {

        User user = userService.getById(LoginUtil.getUserId());
        if (StringUtils.isEmpty(user.getPayPass())){
            throw new RuntimeException("请先设置交易密码");
        }
        String oldStr = DigestUtils.md5Hex(payPass);
        if (!user.getPayPass().equals(oldStr)){
            throw new RuntimeException("交易密码不对");
        }
        LbUserWalletEntity dUser = walletService.getUserWalletInfo(userService.getUserId());
        if (null == dUser || null == dUser.getId()) {
            throw new RuntimeException("您当前没有钱包");
        }
        //添加分红池
        LbSysConfigEntity lbSysConfigEntity = sysConfigService.selectSysConfig();

        if (money.doubleValue() > dUser.getMoney().doubleValue()){
            throw new RuntimeException("您的余额不足,最多可转:"+dUser.getMoney().doubleValue());
        }
        lbSysConfigEntity.setWelfare(BigDecimalUtil.addDouble(lbSysConfigEntity.getWelfare(),money));
        dUser.setMoney(BigDecimalUtil.subDouble(dUser.getMoney(),money));
        if (walletService.updateById(dUser)){
            sysConfigService.updateById(lbSysConfigEntity);
            if ( !userBillService.addBillRecord("爱心基金捐赠",ConfConstants.INTEGRAL_TYPE,0,money,userService.getUserId(),null)){
                throw new RuntimeException("爱心基金捐赠异常");
            }
            return true;
        }


        return false;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean desorbJb() {
        BigDecimal bigDecimal = dao.yestDayAll();
        if (null == bigDecimal || bigDecimal.doubleValue() <= 0){
            log.error(this.getClass().getName()+"---->今日没有佣金发放");
            calculateTzPrice(null);
            return false;
        }
        List<LbUserWalletEntity> userList = dao.getUserList();
        if (null == userList ||userList.isEmpty()){
            calculateTzPrice(null);
            log.error(this.getClass().getName()+"---->今日没有查询到用户释放");
            return false;
        }
        BigDecimal s =  BigDecimalUtil.mul(bigDecimal,new BigDecimal(ConfConstants.INTEGRAL_SF));
        BigDecimal a = BigDecimalUtil.divide(s,new BigDecimal(userList.size()));
        a.setScale(2, BigDecimal.ROUND_HALF_UP);
        if (null == a || a.doubleValue() <= 0){
            calculateTzPrice(null);
            log.error(this.getClass().getName()+"---->今日没有佣金发放");
            return false;
        }
        calculateTzPrice(s);
        for (LbUserWalletEntity bean : userList){
            if (bean.getScoreFreeze().doubleValue() <= a.doubleValue()){
                bean.setScore(BigDecimalUtil.addDouble(bean.getScore(),bean.getScoreFreeze()));
                bean.setScoreFreeze(new BigDecimal("0"));
                if (!userBillService.addBillRecord("释放权益卡",ConfConstants.INTEGRAL_TYPE,1,bean.getScoreFreeze(),bean.getUid(),null)) {
//                throw new CrmebException("释放失败");
                 }
                }else{
                bean.setScore(BigDecimalUtil.addDouble(bean.getScore(),a));
                bean.setScoreFreeze(BigDecimalUtil.subDouble(bean.getScoreFreeze(),a));

                if (!userBillService.addBillRecord("释放权益卡",ConfConstants.INTEGRAL_TYPE,1,a,bean.getUid(),null)) {
//                    throw new CrmebException("释放失败");
                }
                walletService.updateById(bean);
            }
        }

        return true;
    }

    @Override
    public void calculateTzPrice(BigDecimal cal) {
        if (null == cal){
            cal = new BigDecimal(1);
        }else{
            //所有用户可用佣金
            BigDecimal tzTotal = walletService.selectTzTotal();
            //市场流通权益卡
            BigDecimal mkTz = walletService.selectMarketTz();
            BigDecimal divide = BigDecimalUtil.divide(BigDecimalUtil.subDouble(tzTotal, mkTz), new BigDecimal(10));
            if (null != divide){
               if (divide.doubleValue() > 1){
                   cal = divide;
               }
            }else{
                cal = new BigDecimal(1);
            }

        }
        LbTzPriceEntity bean = new LbTzPriceEntity();
        bean.setTime(new Date());
        bean.setCalculate(cal);
        LbSysConfigEntity lbSysConfigEntity = sysConfigService.selectSysConfig();
        if (null == lbSysConfigEntity || lbSysConfigEntity.getOpenZtPrice() == 0){
            bean.setPrice(new BigDecimal(1));
            lbSysConfigEntity.setTzMin(BigDecimal.valueOf(1));
        }else{
            if (cal.doubleValue() <1 ){
                cal = new BigDecimal(1);
            }
            bean.setPrice(cal);
            lbSysConfigEntity.setTzMin(cal);
        }
        if (null == lbSysConfigEntity){
            sysConfigService.save(lbSysConfigEntity);
        }else{
            sysConfigService.updateById(lbSysConfigEntity);
        }
        dao.addTzPrice(bean);
    }

    @Override
    public List<LbTzPriceEntity> selectTzPriceList(Integer limit) {
        return dao.selectTzPrice(limit);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean duiHuanQyk(BigDecimal num,String payPass) {
        LbSysConfigEntity configEntity = sysConfigService.selectSysConfig();
        BigDecimal bigDecimal = dao.addScore();
        BigDecimal djPrice = BigDecimalUtil.divide(configEntity.getDividendPool(),bigDecimal);
        BigDecimal jzk = BigDecimalUtil.mul(djPrice,num);
        User currentUser =userService.getById(LoginUtil.getUserId());
        if (ObjectUtil.isNull(currentUser)) {
            throw new BusinessException("您的登录已过期，请先登录");
        }
        if (configEntity.getDividendPool().doubleValue() <= 0 ){
            throw new RuntimeException("暂不支持闪兑");
        }
        if (org.apache.commons.lang3.StringUtils.isEmpty(currentUser.getPayPass())){
            throw new RuntimeException("请先设置交易密码");
        }
        String oldStr = DigestUtils.md5Hex(payPass);
        if (!currentUser.getPayPass().equals(oldStr)){
            throw new RuntimeException("交易密码不对");
        }
        LbUserWalletEntity dUser = walletService.getUserWalletInfo(userService.getUserId());
        if (null == dUser || null == dUser.getId()) {
            throw new RuntimeException("您当前没有钱包");
        }

        if (num.doubleValue() > dUser.getScore().doubleValue()){
            throw new RuntimeException("闪兑数量不足");
        }
        dUser.setScore(BigDecimalUtil.subDouble(dUser.getScore(),num));
        configEntity.setDividendPool(BigDecimalUtil.subDouble(configEntity.getDividendPool(),jzk));
        walletService.updateById(dUser);
        boolean a =userBillService.saveBill("权益卡兑换", ConfConstants.COMMISSION_TYPE,1, jzk, currentUser.getUserId(),ConfConstants.YJ_FLAG,0);
        if (!a){
            throw new BusinessException("兑换失败");
        }
        a = sysConfigService.updateById(configEntity);
        if (!a){
            throw new BusinessException("兑换失败");
        }
        return a;
    }

    @Override
    public Boolean donateMoney(BigDecimal num, String payPass) {
        LbSysConfigEntity configEntity = sysConfigService.selectSysConfig();
        User currentUser =userService.getById(LoginUtil.getUserId());
        if (ObjectUtil.isNull(currentUser)) {
            throw new BusinessException("您的登录已过期，请先登录");
        }

        if (org.apache.commons.lang3.StringUtils.isEmpty(currentUser.getPayPass())){
            throw new RuntimeException("请先设置交易密码");
        }
        String oldStr = DigestUtils.md5Hex(payPass);
        if (!currentUser.getPayPass().equals(oldStr)){
            throw new RuntimeException("交易密码不对");
        }
        LbUserWalletEntity dUser = walletService.getUserWalletInfo(userService.getUserId());
        if (null == dUser || null == dUser.getId()) {
            throw new RuntimeException("您当前没有钱包");
        }

        if (num.doubleValue() > dUser.getMoney().doubleValue()){
            throw new RuntimeException("佣金余额不足");
        }
        dUser.setMoney(BigDecimalUtil.subDouble(dUser.getMoney(),num));
        configEntity.setDividendPool(BigDecimalUtil.addDouble(configEntity.getDividendPool(),num));
        walletService.updateById(dUser);
        boolean a =userBillService.addBillRecord("基金捐赠", ConfConstants.COMMISSION_TYPE,0, num, currentUser.getUserId(),0);
        if (!a){
            throw new BusinessException("兑换失败");
        }
        a = sysConfigService.updateById(configEntity);
        if (!a){
            throw new BusinessException("兑换失败");
        }
        return a;
    }

    @Override
    public Map<String, Object> getQykInfo() {
        Map<String,Object> map = new HashMap<>();
        LbSysConfigEntity configEntity = sysConfigService.selectSysConfig();
        BigDecimal bigDecimal = dao.addScore();
        BigDecimal djPrice = new BigDecimal(0);
        if (bigDecimal.doubleValue()>0){
            djPrice = BigDecimalUtil.divide(configEntity.getDividendPool(),bigDecimal);
        }

        map.put("qykNum",bigDecimal);//持有权益卡数量
        map.put("qykDj",djPrice);//权益卡单价
        map.put("allNum",131419);//权益卡总数
        map.put("qykSf",configEntity.getQykSf());//权益卡释放数量
        map.put("fhCz",configEntity.getDividendPool());//分红池子
        map.put("dedicate",configEntity.getWelfare());//爱心基金余额
        return map;
    }

    @Override
    public BigDecimal getDonateMoney() {
        LbSysConfigEntity configEntity = sysConfigService.selectSysConfig();
        return configEntity.getDividendPool();
    }
}
